/**
 * @author ning
 */
Cache = new Object();
Cache.Events = new Object();
Cache.Calendars = new Object();
/**
 * _enents 和_calendars 都是id 到Entry 的映射
 * _events[event.id.前缀][event.id]=EventEntry
 * _calendar[generateCategoryId(calendar.id)] =CalendarEntry 
 * 注意eventEntry id的前缀和calendarEntry id的前缀是不一样的。。
 */
Cache._events = new Object();//是一个二维hash表，存放数据如下：Cache._events[calendar][gEventEntry.getId().$t]= "xxx"

Cache._calendars = new Object();//是一个二维hash表，存放数据如下：Cache._events[calendar][gEventEntry.getId().$t]= "xxx"
/**
 * 
 * 把一个event添加到calendar中
 * @param {google.gdata.calendar.CalendarEventEntry} gEventEntry
 * @param {String} calendarID
 */
Cache.Events.add = function(gEventEntry, calendar){
	log ("Cache.Events.add :"+gEventEntry.getId().$t);
	
    if (Cache._events[calendar] == undefined) 
        Cache._events[calendar] = new Object();
    Cache._events[calendar][gEventEntry.getId().$t] = gEventEntry;
}
/**
 * 把一个event添加到calendar中,是对.add  的简易接口。
 * 
 * 这个函数自动解析calendarID
 * @param {google.gdata.calendar.CalendarEventEntry} gEventEntry
 */
Cache.Events.addEvent = function(gEventEntry){
	
	var eid = gEventEntry.getId().$t;
	if (eid== undefined)
	{
		log("eid undefine");
		return ;
	}
		
	var id = eid.lastIndexOf("/full/");
	eid= eid.substring(0,id+5);
	Cache.Events.add(gEventEntry,eid);	
}


/**
 * 
 * @param {String} eventID
 */
Cache.Events.getById = function(eventID){
    for (id in Cache._events) {
        if (Cache._events[id][eventID] != undefined) 
            return Cache._events[id][eventID];
    }
    return null;
}
/**
 * 
 * @param {String} eventID
 */
Cache.Events.remove= function(eventID){
    for (id in Cache._events) {
        if (Cache._events[id][eventID] != undefined) 
            delete Cache._events[id][eventID];
    }
}

Cache.Events.synchronizeToTimeline = function(){//TODO:最好换个位置。
	gEventSource.clear();
    for (ii in Cache._events) {
		events=new Array();
		for (jj in Cache._events[ii])
        	events.push(Cache._events[ii][jj]);
		var cal =  Cache.Calendars.getCalendarOfEvent(ii);
		
//		.getId().getValue();//ii = calendar id 
		color=cal.getColor().getValue();;
		addEventEntriesToTimeline(events);
    }
}

Cache.Events.showEventsOfCalendar= function(selectedCalendarIds){
	gEventSource.clear();
	for (tt =  0 ;tt< selectedCalendarIds.length;tt++)
	{
		id = selectedCalendarIds[tt];
		var flag=false;
		
	    for (ii in Cache._events) {
			if (ii.indexOf(id) == -1)
				continue;
			events=new Array();
			for (jj in Cache._events[ii])
	        	events.push(Cache._events[ii][jj]);
			var cal =  Cache.Calendars.getCalendarOfEvent(ii);

			color=cal.getColor().getValue();;
			addEventEntriesToTimeline(events);
			flag=true;
			break;
	    }
		if (!flag)
		{
			getEvents(Cache.Calendars.getById(id).getAtomAlternateLink().getHref(),'uesless color');
		}
	}
}

/**
 * 
 */
Cache.Events.clear = function(){
    Cache._events = new Object();
}
////////////////////////////////////////////////////////////////////////////////////
//calendar 操作
Cache.Calendars.add = function(gCalendarEntry){
	//modified by pengpai, 2008.12.7
	var id = generateCategoryId(gCalendarEntry.getId().getValue());
    //if (Cache._calendars[id] == undefined) 
        Cache._calendars[id] = gCalendarEntry;
}

Cache.Calendars.getById = function(calendarID){
    if (Cache._calendars[calendarID] != undefined) 
        return Cache._calendars[calendarID];
    return null;
}
Cache.Calendars.clear= function(){
	Cache._calendars=new Object();
}
Cache.Calendars.getAllCalendars= function(){
	var rst=new Array();
	for (id in Cache._calendars) {
		rst.push(Cache._calendars[id]);
    }
	return rst;
}


/**
 * 在timelin-about 中showEditEventDialog需要。
 * @param {String} eventID
 */
Cache.Calendars.getCalendarOfEvent= function(eventID){//TODO :MOVE
    for (id in Cache._calendars) {
		//perfix=Cache._calendars[id].getAtomAlternateLink().getHref();
		
		if (eventID.indexOf(id)!=-1)
            return Cache._calendars[id];
    }
}




